<?php
namespace app\index\controller;

use think\Request;

class File
{
    public function save()
    {
        if (Request::instance()->isOptions()) {
            echo json_encode([
                "code" => 200
            ]);
            exit;
        }

        // header("Content-Type: application/json");

        // 
        // echo json_encode($_POST);exit;

        // name=jack&age=12
        // $data = file_get_contents("php://input");
        // echo $data;exit;

        // array(1) { ["file"]=> array(5) { ["name"]=> string(14) "basketball.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(22) "C:\Windows\phpADAD.tmp" ["error"]=> int(0) ["size"]=> int(24101) } }
        // echo json_encode($_FILES["file"]);exit;

        try {
            $file = $_FILES['file'];
            $fileName = $file['name'];
            $urlPath = "/uploads/" . $fileName;
            $destFile = ROOT_PATH . "/public" . $urlPath;
            move_uploaded_file($file["tmp_name"], $destFile);
            echo json_encode(["location" => "http://www.tp505.cm" . $urlPath, "ROOT_PATH" => ROOT_PATH]);
            exit;
            
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }

    }

    public function saveUdetior()
    {
        if (Request::instance()->isOptions()) {
            echo json_encode([
                "code" => 200
            ]);
            exit;
        }

        try {
            $file = $_FILES['file'];
            $fileName = $file['name'];
            $urlPath = "/uploads/" . $fileName;
            $destFile = ROOT_PATH . "/public" . $urlPath;
            move_uploaded_file($file["tmp_name"], $destFile);

            header("Content-Type: application/json");
            echo json_encode(["location" => "http://www.tp505.cm" . $urlPath, "ROOT_PATH" => ROOT_PATH]);
            exit;
            
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }

    }

    public function saveFormData()
    {
        // header("Content-Type: application/json");
        
        // "php://input": enctype="multipart/form-data" 时无效。
        
        // $data = file_get_contents("php://input");
        // echo $data;exit;

        echo json_encode($_POST);exit;
    }
}
